﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Spanner;

namespace SpannerWebSite
{
    /// <summary>
    /// Utility base class for the Spanner reference docs.
    /// </summary>
    internal class Reference : CommonUI
    {
        //internal class ReferenceSection
        //{
        //    internal string Title;
        //    internal string ID;
        //    internal Html Docs;
        //    internal ReferenceDemo[] ReferenceDemoes;
        //}

        internal static Html Content; // Global set by ReferenceDemo.GenContent actions.

        internal static Act InitAct; // Global set by ReferenceDemo.GenContent actions.

        internal static Model[] Dependencies; // Global set by ReferenceDemo.GenContent actions.

        //internal class ReferenceDemo
        //{
        //    internal string Subtitle;
        //    internal Html Doc;
        //    internal string ID;
        //    internal Action GenContent; // This code should start with '// [ID]' and end with '// END'.
        //    internal string SrcFilePath;
        //}

        //internal static ReferenceDemo RefDemo(string subtitle, Html doc, string id, Action genContent, [CallerFilePath] string srcFilePath = "") {
        //    return new ReferenceDemo {
        //        Subtitle = subtitle,
        //        Doc = doc,
        //        ID = id,
        //        GenContent = genContent,
        //        SrcFilePath = srcFilePath
        //    };
        //}

        internal static Html CodeFigures(
            string id,
            Action genContent,
            [CallerFilePath] string srcFilePath = "",
            bool skipHtml = false,
            bool skipJS = false,
            string startJSFrom = null,
            bool includeTryItLink = true
        )
        {
            const string path = Const.DemoWebSitePath;
            genContent(); // This sets up Content and InitAct.
            var html = StandardSpannerHtmlPage(id, Content ?? "");
            var webPage = WebPage(id, html, InitAct, Dependencies);
            var ctxt = new Context(path);
            ctxt.WriteWebPage(webPage);
            Content = null;
            InitAct = null;
            Dependencies = null;

            var csPath = srcFilePath;
            var csStart = "// " + id;
            var csEnd = "// END";
            var csFig = FileFragmentFigure("Handwritten C#", Lang.csharp, csPath, csStart, csEnd, skipFirstLine: true);

            var htmlPath = path + @"\html\" + id + ".html";
            var htmlStart = "<body>";
            var htmlEnd = "<script src";
            var htmlFig =
                ( skipHtml
                ? ""
                : FileFragmentFigure("Generated HTML", Lang.markup, htmlPath, htmlStart, htmlEnd,
                    skipFirstLine: true, ignoreUnindentedLines: false)
                );

            var jsPath = htmlPath;
            var jsStart = startJSFrom ?? ("function " + id + "() {");
            var jsEnd = "spanner.windowOnLoad";
            var jsFig =
                ( skipJS
                ? ""
                : FileFragmentFigure("Generated JavaScript", Lang.javascript, jsPath, jsStart, jsEnd)
                );

            var tryItLink = P(A("[Try it.]").WithAttr("href",
                htmlPath.Replace(Const.WebSitePath, "..").Replace("\\", "/")));

            return Group(csFig, htmlFig, jsFig, includeTryItLink ? tryItLink : "");
        }
    }
}
